﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Security_TestTree : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Treeview1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        if (IsCallback)
        {
            if (e.Node.ChildNodes.Count == 0)
            {
                LoadChildNode(e.Node);
            }
        }
    }
    private void LoadChildNode(TreeNode node)
    {
        DirectoryInfo directory;
        directory = new DirectoryInfo(node.Value);
        foreach (DirectoryInfo sub in directory.GetDirectories())
        {
            TreeNode subNode = new TreeNode(sub.Name);
            subNode.Value = sub.FullName;
            try
            {
                if (sub.GetDirectories().Length > 0 || sub.GetFiles().Length > 0)
                {
                    subNode.SelectAction = TreeNodeSelectAction.Expand;
                    subNode.PopulateOnDemand = true;
                    subNode.NavigateUrl = "javascript:void(0);";
                }
            }
            catch
            {
                subNode.ImageUrl =
                    "WebResource.axd?a=s&r=TreeView_XP_Explorer_ParentNode.gif&t=632242003305625000";
            }
            node.ChildNodes.Add(subNode);
        }

        foreach (FileInfo fi in directory.GetFiles())
        {
            TreeNode subNode = new TreeNode(fi.Name);
            subNode.SelectAction = TreeNodeSelectAction.Expand;
            subNode.NavigateUrl = "javascript:void(0);";
            subNode.Value = fi.FullName;
            subNode.PopulateOnDemand = false;
            node.ChildNodes.Add(subNode);
        }
    }
}
